package ar.fiuba.tp7552.buildadmin.webactions.gastos;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;

import ar.fiuba.tp7552.buildadmin.model.builds.Edificio;
import ar.fiuba.tp7552.buildadmin.model.gastos.Gasto;
import ar.fiuba.tp7552.buildadmin.model.gastos.GastoCriteria;
import ar.fiuba.tp7552.buildadmin.model.gastos.TipoGasto;
import ar.fiuba.tp7552.buildadmin.service.EdificioService;
import ar.fiuba.tp7552.buildadmin.service.GastoService;
import ar.fiuba.tp7552.buildadmin.service.PermissionType;
import ar.fiuba.tp7552.buildadmin.webactions.BaseAction;

import com.opensymphony.xwork2.Preparable;

public class BuscarGastoAction extends BaseAction implements Preparable {
	
	private GastoService gastoService;
	private EdificioService edificioService;
	private GastoCriteria criteria = new GastoCriteria();
	private Collection<Gasto> gastos;
	private Map<Edificio, Collection<TipoGasto>>edificios;
	
	// ~~ Constructor ====================================================
	
	public BuscarGastoAction(GastoService gastoService, EdificioService edificioService) {
		super();
		this.gastoService = gastoService;
		this.edificioService = edificioService;
	}
	
	// ~~ Actions ========================================================
	
	@Override
	public void prepare() {
		
	}
	
	public String load() {
		return BaseAction.SUCCESS;
	}
	
	public String  search() {
		gastos = gastoService.findByCriteria(criteria);
		return BaseAction.SUCCESS;
	}
	
	// ~~ getters y setters ========================================================
	
	public Collection<Gasto> getGastos() {
		return gastos;
	}
	
	public GastoCriteria getCriteria() {
		return criteria;
	}

	public Map<Edificio, Collection<TipoGasto>> getEdificios() {
		if (edificios == null) {
			edificios = new LinkedHashMap<Edificio, Collection<TipoGasto>>();
			for (Edificio edificio : edificioService.getAll(PermissionType.GASTO)) {
				edificios.put(edificio, edificio.getTipoGastos());	
			}
		}
		return edificios;
	}
}